// pages/index/register/register.js
var areaList = require('../../../utils/area.js').default; 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    columns: ['博士研究生', '硕士研究生', '本科', '大专', '高中', '中专', '初中', '小学及以下'],
    areaList: areaList,
    showEducation: false,
    showRegion: false,
    loading: false,
    noName: '',
    noPhone: '',
    name: '',
    phone: '',
    college: '',
    region: '',
    education: '',
  },
  showEducation() {
    this.setData({ showEducation: true })
  },
  showRegion() {
    this.setData({ showRegion: true })
  },
  onCancel() {
    this.setData({ showEducation: false, showRegion: false })
  },
  onChooseEducation(event) {
    const { picker, value, index } = event.detail;
    this.setData({ education: value, showEducation: false,  })
  },
  onChangeEducation(event) {
    const { picker, value, index } = event.detail;
    this.setData({ education: value })
  },
  onChangeRegion(event) {
    const { values, indexs } = event.detail;
    this.setData({ region: values[0].name + values[1].name + values[2].name })
    this.setData({ showRegion: false })
  },
  getName(e) {
    if (e.detail) this.setData({ noName: '' });
  },
  getPhone(e) {
    if (e.detail) this.setData({ noPhone: '' });
  },
  submit() {
    if (this.data.loading) return false;
    let err = false;
    if (!this.data.name) {
      this.setData({ noName: '请输入姓名' });
      err = true;
    }
    if (!this.data.phone) {
      this.setData({ noPhone: '请输入手机号码' });
      err = true;
    } else {
      var regPhone = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[8-9]{1}))+\d{8})$/;
      if (this.data.phone.length < 11 || !regPhone.test(this.data.phone)) {
        this.setData({ noPhone: '手机号格式错误' });
        err = true;
      }
    }
    if (err) return false;
    wx.login({
      success: code => {
        this.setData({ 
          code: code.code, 
          loading: true, 
          register_time: wx.formatTime(new Date(), 'd-t')
        });
        wx.post('register', this.data, res => {
          if (res) {
            wx.toast('注册成功', 'success');
            setTimeout(() => {
              wx.redirectTo({
                url: '../home/home',
              })
            }, 1000);
          } else {
            wx.toast('用户已存在');
            wx.redirectTo({
              url: '../home/home',
            })
          }
        })
      }
    })
  },
  call() {
    wx.copyright();
  }
})